package com.zl.data.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.zl.data.config.DataConstant;

/**
 * @author ZL @朱林</a>
 * @Version 1.0
 * @Description TODO
 * @date 2021/01/11  15:01
 */
public class DataUtil {
    private static DataUtil INSTANCE;
    private DataOpenHelper dataOpenHelper;
    private SQLiteDatabase database;

    public static DataUtil getInstance(Context mContext) {
        if (INSTANCE == null) {
            synchronized (DataUtil.class) {
                if (null == INSTANCE) {
                    INSTANCE = new DataUtil(mContext);
                }
            }
        }
        return INSTANCE;
    }

    public DataUtil(Context mContext) {
        Log.i(DataConstant.TAG, "DataUtil;onCreate");
        dataOpenHelper = new DataOpenHelper(mContext);
    }


    public void onDestroy() {
        dataOpenHelper.close();
    }

    public SQLiteDatabase getDatabase() {
        if (null == database) {
            synchronized (DataUtil.class) {
                if (null == database) {
                    Log.i(DataConstant.TAG, "DataUtil;getDatabase;dataOpenHelper:" + dataOpenHelper);
                    database = dataOpenHelper.getWritableDatabase();
                }
            }
        }
        return database;
    }

    public boolean replace(String key, byte[] value) {
        ContentValues values = DataBeanInfo.putValues(new DataBean(key, value));
        long rowId = getDatabase().replace(DataBeanInfo.TABLE_NAME, null, values);
        if (rowId == -1) {
            return false;
        } else {
            return true;
        }
    }

    public boolean insert(String key, byte[] value) {
        ContentValues values = DataBeanInfo.putValues(new DataBean(key, value));
        long rowId = getDatabase().insert(DataBeanInfo.TABLE_NAME, null, values);
        if (rowId == -1) {
            return false;
        } else {
            return true;
        }
    }

    public boolean update(String key, byte[] value) {
        ContentValues values = DataBeanInfo.putValues(new DataBean(key, value));
        int rows = getDatabase().update(DataBeanInfo.TABLE_NAME, values, DataBeanInfo.KEY + "=?", new String[]{key});
        if (rows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public byte[] query(String key) {
        try {
            Cursor cursor = getDatabase().query(DataBeanInfo.TABLE_NAME, null, DataBeanInfo.KEY + "=?", new String[]{key}, null, null, null);
            DataBean dataBean = null;
            if (cursor != null) {
                if (cursor.moveToNext()) {
                    dataBean = DataBeanInfo.getValues(cursor);
                }
                cursor.close();
            }
            if (null == dataBean) {
                return null;
            }else {
                return dataBean.getValue();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            Log.e(DataConstant.TAG, "query error:" + ex.getMessage());
        }
        return null;
    }

    public boolean delete(String key) {
        int rows = getDatabase().delete(DataBeanInfo.TABLE_NAME, DataBeanInfo.KEY + "=?", new String[]{key});
        if (rows > 0) {
            return true;
        } else {
            return false;
        }
    }

}
